/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
'use strict';

import * as nodes from 'vscode-css-languageservice/parser/cssNodes';
import { LintSettings } from 'vscode-css-languageservice/cssLanguageService';

const Warning = nodes.Level.Warning;
const Error = nodes.Level.Error;
const Ignore = nodes.Level.Ignore;



export class Rule implements nodes.IRule {

	public constructor(public id: string, public message: string, public defaultValue: nodes.Level) {
		// nothing to do
	}
}
export let Rules = {
	AllVendorPrefixes: new Rule('compatibleVendorPrefixes', "When using a vendor-specific prefix make sure to also include all other vendor-specific properties", Ignore),
	IncludeStandardPropertyWhenUsingVendorPrefix: new Rule('vendorPrefix', "When using a vendor-specific prefix also include the standard property", Warning),
	DuplicateDeclarations: new Rule('duplicateProperties', "Do not use duplicate style definitions", Ignore),
	EmptyRuleSet: new Rule('emptyRules', "Do not use empty rulesets", Warning),
	ImportStatemement: new Rule('importStatement', "Import statements do not load in parallel", Ignore),
	BewareOfBoxModelSize: new Rule('boxModel', "Do not use width or height when using padding or border", Ignore),
	UniversalSelector: new Rule('universalSelector', "The universal selector (*) is known to be slow", Ignore),
	ZeroWithUnit: new Rule('zeroUnits', "No unit for zero needed", Ignore),
	RequiredPropertiesForFontFace: new Rule('fontFaceProperties', "@font-face rule must define 'src' and 'font-family' properties", Warning),
	HexColorLength: new Rule('hexColorLength', "Hex colors must consist of three, four, six or eight hex numbers", Error),
	ArgsInColorFunction: new Rule('argumentsInColorFunction', "Invalid number of parameters", Error),
	UnknownProperty: new Rule('unknownProperties', "Unknown property.", Warning),
	IEStarHack: new Rule('ieHack', "IE hacks are only necessary when supporting IE7 and older", Ignore),
	UnknownVendorSpecificProperty: new Rule('unknownVendorSpecificProperties', "Unknown vendor specific property.", Ignore),
	PropertyIgnoredDueToDisplay: new Rule('propertyIgnoredDueToDisplay', "Property is ignored due to the display.", Warning),
	AvoidImportant: new Rule('important', "Avoid using !important. It is an indication that the specificity of the entire CSS has gotten out of control and needs to be refactored.", Ignore),
	AvoidFloat: new Rule('float', "Avoid using 'float'. Floats lead to fragile CSS that is easy to break if one aspect of the layout changes.", Ignore),
	AvoidIdSelector: new Rule('idSelector', "Selectors should not contain IDs because these rules are too tightly coupled with the HTML.", Ignore),
};

export class LintConfigurationSettings {
	constructor(private conf: LintSettings = {}) {
	}

	get(rule: Rule): nodes.Level {
		if (this.conf.hasOwnProperty(rule.id)) {
			let level = toLevel(this.conf[rule.id]);
			if (level) {
				return level;
			}
		}
		return rule.defaultValue;
	}
}

function toLevel(level: string): nodes.Level {
	switch (level) {
		case 'ignore': return nodes.Level.Ignore;
		case 'warning': return nodes.Level.Warning;
		case 'error': return nodes.Level.Error;
	}
	return null;
}
